import axios from 'axios'
import { message } from 'ant-design-vue'
import { getStore } from "./mUtils";
// console.log(process.env.VUE_APP_BASE_API);

const service = axios.create({
  baseURL: process.env.VUE_APP_BASE_API,
  // baseURL: 'http://192.168.1.66:8001/',
  timeout: 50000, // request timeout
  headers: {
    // 'session': sessionStorage.getItem('session'),
    "Content-Security-Policy" : "upgrade-insecure-requests"
  }
})

service.interceptors.request.use(
  config => {
    const token = getStore('sessionId')
    token ? config.headers.Authorization = token : null
    return config
  },
  error => {
    return Promise.reject(error)
  }
)

service.interceptors.response.use(
  response => {
    const res = response.data
    if (res.code != 200) {
      if (res.code == 403) {
        top.window.location = `${process.env.VUE_APP_CAS_URL}?service=${process.env.VUE_APP_PUBLIC_URL}`
      }
      message.error(res.message)
      return Promise.reject('error')
    } else {
      return response
    }
  }, error => {
    let mes = ''
    if (error.response.data.error) {
      mes = error.response.data.error
    } else mes = error.response.data.message
    console.log(error.response)

    return Promise.reject(error)
  })

export default service
